// import axios from 'axios'
// import axios from '@/utils/request';
import { QueryAttchmentByBusinessId ,QueryAttchmentByExtendId,UploadFile, UploadFileStream, DeleteAttachment} from '@/api/attchment';

export default {
    data() {
      return {
         //弹层图上url
        dialogImageUrl: '', 
         //弹层对话框
        dialogVisible: false,
         //是否展示删除
        removeDisable: false,
        //是否隐藏上传
        hideUpload: false, 
        //文件对像数组
        fileList: [], 
        // 小图全路径
        smallAllPath: '',
        // 文件全路径
        allPath: '',
        // 文件名称
        fileName:'',
        //上传文件数量
        limit:5,
   
      }
    },
    methods: {
      // 生成随机guid
      guid() {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
          var r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8)
          return v.toString(16)
        })
      },

      //根据业务Id查询所有附件
      queryBusinessId(id){
        QueryAttchmentByBusinessId(id).then((res)=>{
          
          this.fileList = [];
          res.map((d)=>{
            this.fileList.push({
              id: d.id,
              attType:d.attType,
              url: d.fileAllPath,
              fileName: d.fileName,
              businessId: d.businessId,
              extendId:d.extendId,
              orderIndex: d.orderIndex,
            });
          });

          //设置上传图片按钮是否可见
          if(this.fileList!=null && this.fileList!=undefined){
            this.hideUpload = this.fileList.length >= this.limit;
          }else{
            this.hideUpload =false;
          }
        });
      },

      //根据扩展Id查询附件
      queryExtendId(id){
        QueryAttchmentByExtendId(id).then((res)=>{
          console.log("id",id,"res",res)

          this.fileList = [];
          res.map((d)=>{
            this.fileList.push({
              id: d.id,
              attType:d.attType,
              url: d.fileAllPath,
              fileName: d.fileName,
              businessId: d.businessId,
              extendId:d.extendId,
              orderIndex: d.orderIndex,
            });
          });

          //设置上传图片按钮是否可见
          if(this.fileList!=null && this.fileList!=undefined){
            this.hideUpload = this.fileList.length >= this.limit;
          }else{
            this.hideUpload =false;
          }
        });
      },

      //删除图片接口
      deleteAttchment(id, businessId,extendId,fileList) {
        if(id){
          DeleteAttachment(id).then(res => {
            if (res) {
              this.$message('删除成功!');
              this.$refs.upload.uploadFiles = [];
              this.close();
              if(extendId!=null && extendId!=''&& extendId!=undefined){
                this.queryExtendId(extendId);
              }else{
                this.queryBusinessId(businessId)
              }
            } else {
                this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.$message.error(e.message);
          })
        }
        else{
          if(extendId!=null && extendId!=''&& extendId!=undefined){
            this.queryExtendId(extendId);
          }else{
            this.queryBusinessId(businessId)
          }
        }
      },

      // 上传图片
      uploadImg(obj) {
        let fdata = new FormData()
        fdata.set('file', obj.file)
        fdata.set('imgType', obj.imgType); //图片类型
        fdata.set('attType', obj.attType); //文件类型
        fdata.set('fileName', obj.fileName); // 文件名称
        fdata.set('orderIndex', obj.orderIndex); // 顺序
        fdata.set('businessId', obj.businessId); // 业务ID
        if (obj.extendId != undefined) {
          fdata.set('extendId', obj.extendId); // 扩展Id
        }
       
      //上传文件 
      UploadFile(fdata).then((res) => {
          this.fileName = res.fileName;
          this.allPath = res.fileAllPath;
          this.smallAllPath = res.fileSmallAllPath;

          if(this.fileList!=null && this.fileList!=undefined){
            this.hideUpload = this.fileList.length >= this.limit;
          }else{
            this.hideUpload =false;
          }
        }).catch(e => {
          this.$message.error(e.message)
        })
      },

      //上传文件流
      uploadFile(obj) {
        let fdata = new FormData()
        fdata.append('file', obj.file)
        let arr = {
          "fileName": obj.fileName,//文件名称
          "attType": obj.attType,//文件类型(1:图片,2:文件)
          "orderIndex": obj.orderIndex,//顺序
          "businessId": obj.businessId,// 业务ID
        }
        if (obj.extendId != undefined) {
          arr.extendId = obj.extendId//车牌号
        }
      
        var str = JSON.stringify(arr);
        UploadFileStream(str,fdata).then((res) => {
          this.fileName = res.fileName;
          this.allPath = res.fileAllPath;
          this.smallAllPath = res.fileSmallAllPath;
          //设置上传图片按钮是否可见
          if(this.fileList!=null && this.fileList!=undefined){
            this.hideUpload = this.fileList.length >= this.limit;
          }else{
            this.hideUpload =false;
          }
        }).catch(e => {
            this.$message.error(e.message)
        })
      },

      // 删除图片
      handleRemove(file,fileList) {
        if (file) {
          this.removeConfirm(file.id, file.businessId,file.extendId,fileList);
        }
      },

      // 删除确认
      removeConfirm(id, businessId,extendId,fileList) {
        this.$confirm('是否确认删除该图片?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 执行删除操作
          this.deleteAttchment(id, businessId,extendId,fileList)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        })
      },

      //只能上传一份文件
      handleExceed(files, fileList) {
        this.$message.warning(`只能上传一份文件！`);
      },

      //关闭
      close() {
        this.fileList = [];
        this.hideUpload = this.fileList.length >= 1;
      },

      // 取消按钮
      cancel() {
        this.close();
      },

      // 点击 X 关闭对话框的回调
      handleDialogClose(val) {
        this.close();
      },

      // 放大查看图片
      handlePictureCardPreview(file) {
        this.dialogVisible = true;
        this.dialogImageUrl = file.url;
      },
    }
  }